package org.example.book.dutychain;

import org.example.book.pojo.BusinessLaunch;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CityHandler extends AbstractBusinessHandler{
    @Override
    public List<BusinessLaunch> processHandler(List<BusinessLaunch> launchList, String targetCitry, String targetSex, String targetProduct) {
        if (launchList.isEmpty()) {
            return launchList;
        }

        launchList = launchList.stream().filter(launch -> {
            String city = launch.getTargetCity();
            if (!StringUtils.hasText(city)) {
                return true;
            }
            List<String> cities = Arrays.asList(city.split(","));
            return cities.contains(targetCitry);
        }).collect(Collectors.toList());
        if (hasNextHandler()) {
            return nextHandler.processHandler(launchList, targetCitry, targetSex, targetProduct);
        }
        return launchList;
    }
}
